package autoz.core.markers;

import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;

import autoz.core.problems.UmlProblem;
/**
 * 
 * @author jimmy
 * @see Inspired by org.eclipse.epsilon.hutn.dt.markers.MarkerManager
 */
public class MarkerManager {
	private static final String MARKER_ID = "autoz.core.inconsistency";
	
	private final IResource resource;
	
	public MarkerManager(IResource resource) {
		this.resource = resource;
	}
	
	public void replaceErrorMarkers(List<UmlProblem> problems) throws CoreException {
		removeMarkers();
		addErrorMarkers(problems);
	}
	
	private void addErrorMarkers(List<UmlProblem> problems) throws CoreException {
		for (UmlProblem problem : problems)
			addErrorMarker(problem);
	}
	
	private void addErrorMarker(UmlProblem problem) throws CoreException {
		addErrorMarker(problem.getProblem(), problem.getUMLLocation());
	}
	
	private void addErrorMarker(String error, String location) throws CoreException {
		final IMarker marker = resource.createMarker(MARKER_ID);
		marker.setAttribute(IMarker.MESSAGE, error);
		marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
		marker.setAttribute(IMarker.LOCATION, location);
	}
	
	public void removeMarkers() throws CoreException {
		resource.deleteMarkers(MARKER_ID, true, IResource.DEPTH_ZERO);
	}

	public static void clearErrorMarkers(IFile iFile) throws CoreException {
		iFile.deleteMarkers(MARKER_ID, true, IResource.DEPTH_ZERO);
		
	}

}
